package com.ai.sb.service.impl;

import com.ai.sb.dao.SbRecordDicMapper;
import com.ai.sb.dao.SbRecordMapper;
import com.ai.sb.dao.SbUserMapper;
import com.ai.sb.model.SbRecord;
import com.ai.sb.model.SbRecordDic;
import com.ai.sb.model.SbUser;
import com.ai.sb.service.SbRecordService;
import com.ai.sb.core.AbstractService;
import com.ai.sb.util.BaseUtil;
import com.ai.sb.util.Result;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * Created by CodeGenerator on 2018/06/09.
 */
@Service
@Transactional
public class SbRecordServiceImpl extends AbstractService<SbRecord> implements SbRecordService {
    private Logger logger = LoggerFactory.getLogger(SbRecordServiceImpl.class);
    @Resource
    private SbRecordMapper sbRecordMapper;
    @Resource
    private SbRecordDicMapper sbRecordDicMapper;
    @Resource
    private SbUserMapper sbUserMapper;


    @Override
    public Result addRecord(SbRecord sbRecord) {
        logger.info("添加点击记录，{}",sbRecord);
        SbRecordDic sbRecordDic = sbRecordDicMapper.selectByPrimaryKey(sbRecord.getDicId());
        logger.info("查询点击按钮类型，{}",sbRecordDic);
        Date now = new Date();
        if(sbRecordDic.getIsTask()){
            logger.info("按钮类型为任务");
            StringBuffer str = new StringBuffer();
            str.append(sbRecord.getUserId());
            str.append(sbRecord.getDicId());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String date = sdf.format(now);
            str.append(date);
            sbRecord.setrUk(str.toString());
            logger.info("添加唯一约束键，{}",str.toString());
        }
        sbRecord.setId(BaseUtil.getUUID());
        sbRecord.setRecordTime(now);
        int i = sbRecordMapper.insert(sbRecord);
        logger.info("插入结果，{}",i);
        return Result.success(i);
    }

    @Override
    public Result selTask(String userId,String recordType) {
        logger.info("查询当前任务，当前平台：{}，用户ID：{}",recordType,userId);
        logger.info("查询当前环境的任务集合");
        SbRecordDic qs = new SbRecordDic();
        qs.setIsTask(true);
        qs.setRecordType(recordType);
        List<SbRecordDic> sbRecordDicList = sbRecordDicMapper.select(qs);
        logger.info("查询集合结果：{}",sbRecordDicList);
        Date now = new Date();
        boolean isOk = true;
        for (SbRecordDic sbRecordDic : sbRecordDicList) {
            StringBuffer str = new StringBuffer();
            str.append(userId);
            str.append(sbRecordDic.getId());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String date = sdf.format(now);
            str.append(date);
            SbRecord qs2 = new SbRecord();
            qs2.setrUk(str.toString());
            logger.info("使用唯一约束查询记录，参数：",qs2);
            SbRecord rs = sbRecordMapper.selectOne(qs2);
            logger.info("查询结果：{}",rs);
            if(rs!=null){
                sbRecordDic.setFinish(true);
            }else{
                isOk = false;
            }
        }
        if(isOk){
           SbUser sbUser = sbUserMapper.selectByPrimaryKey(userId);
           logger.info("按主键查询用户，{}",sbUser);
            boolean b = false;
           if(recordType.equals("wx")){
               b = this.isSame(sbUser.getLastTimeWx());
               if(!b)sbUser.setSbWxCount(3);
               if(!b)sbUser.setLastTimeWx(new Date());
           }else if(recordType.equals("app")){
               b = this.isSame(sbUser.getLastTimeApp());
               if(!b)sbUser.setSbAppCount(3);
               if(!b)sbUser.setLastTimeApp(new Date());
           }
           logger.info("任务"+recordType+"全部完成，设置碎冰数为3,更新完成时间,参数{}",sbUser);
            int i = 0;
            if(!b){
                i = sbUserMapper.updateByPrimaryKeySelective(sbUser);
            }
           logger.info("用户碎冰数及时间修改：{}",i);
        }
        logger.info("查询当天用户当前平台的任务完成情况，结果：{}",sbRecordDicList);
        return Result.success(sbRecordDicList);
    }

    private boolean isSame(Date date){
        Date currentTime = new Date();//当前时间
        if(date==null) return false;
        if (DateUtils.isSameDay(currentTime, date)){
            return true;
        }else{
            return false;
        }
    }

}
